<?php
/**
 * View360
 *
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;
class View360Controller extends BaseController {

	/*客户基本信息*/
	public function index(){
		$res = array('rcode'=>0,'msg'=>'OK');
		$customer_id = I('get.customer_id',false,'intval');
		$date = date('Y-m-d H:i:s');
		//客户信息top
		$wheres1 = array();
		$wheres1['a.customer_id'] = array('eq',$customer_id);
		$wheres1['_string'] = '1=1';
		$custoInfo = M()->field("a.customer_id,a.nickname as customername,a.phone,a.location,a.mount,a.customer_code,
						a.qq,a.weixin_id,a.career,a.education,a.birthday,a.address,a.remark,a.source_id,a.sales_person,
						d.nickname as sales_name,e.sector_name as sub_company_name,f.customer_id as combineto_id,
						f.nickname as combineto_name,a.combine_time")
					->table('customer as a')
					->join('LEFT JOIN user as d on d.user_id = a.sales_person','LEFT')
					->join('LEFT JOIN sector as e on e.sector_id = a.sales_sub_company','LEFT')
					->join('LEFT JOIN customer as f on f.customer_id = a.combine_to','LEFT')
					->where($wheres1)
					->find();

		if($custoInfo['combine_time']){
			$custoInfo['combine_time'] = date('Y-m-d',strtotime($custoInfo['combine_time']));
		}
		//合并到该客户的客户信息
		$map = array();
		$map['combine_to'] = array('eq',$customer_id);
		$combinFromInfos = M('Customer')->field('customer_id,nickname,combine_time')->where($map)->select();
		for ($n=0; $n < count($combinFromInfos); $n++) { 
			if($combinFromInfos[$n]['combine_time']){
				$combinFromInfos[$n]['combine_time'] = date('Y-m-d',strtotime($combinFromInfos[$n]['combine_time']));
			}
		}
		//服务中合同
	   	$combinFromInfo = D('Customer')->getCustIdAll($customer_id);	//获取当前客户及其归并客户id
	   	$wheres2 = array();
   		$wheres2['contract.customer_id'] = array('in',$combinFromInfo);
   		$wheres2['contract.state'] = array('eq',1);
	   	$onServicecCntract = M('contract')
	   						->field('p.name as productname,contract.start_time,contract.end_time,contract.mount,
	   						contract.state,contract.contract_id,contract.customer_id,contract.order_num')
	   						->join("LEFT JOIN product p on p.product_id=contract.product_id")
	   						->where($wheres2)
	   						->select();
	   	for ($j=0; $j < count($onServicecCntract); $j++) {
	   		$onServicecCntract[$j]['remainDays'] = D('Customer')->diffBetweenTwoDays($onServicecCntract[$j]['end_time'],date('Y-m-d H:i:s'));
	   		$onServicecCntract[$j]['start_time'] = date('Y-m-d',strtotime($onServicecCntract[$j]['start_time']));
	   		$onServicecCntract[$j]['end_time'] = date('Y-m-d',strtotime($onServicecCntract[$j]['end_time']));
	   		//去掉合同金额后面的'.00'
            if($onServicecCntract[$j]['mount'] && substr($onServicecCntract[$j]['mount'], -3) == '.00'){
                $onServicecCntract[$j]['mount'] = floor($onServicecCntract[$j]['mount']);
            }
	   	}
	   	//最近两次付款
	   	$payInfo = D('Customer')->getPayInfos($customer_id);
	   	if($payInfo && count($payInfo)>=2){
		   	for ($m=0; $m < 2; $m++) {
		   		$payInfoTwoDate[] = $payInfo[$m];
		   	}
	   	}else if($payInfo && count($payInfo)<2){
	   		for ($m=0; $m < count($payInfo); $m++) {
		   		$payInfoTwoDate[] = $payInfo[$m];
		   	}
	   	}else{
	   		$payInfoTwoDate = array();
	   	}
	   	//最近两次合同
	   	$contractInfo = D('Customer')->getContractInfos($customer_id);
	   	if($contractInfo[0] && count($contractInfo[0])>=2){
		   	for ($n=0; $n < 2; $n++) {
		   		$contractInfoTwoDate[] = $contractInfo[0][$n];
		   	}
		   	//累积服务时长
		   	$servicedDay = $contractInfo[1];
	   	}else if($contractInfo[0] && count($contractInfo[0])<2){
	   		for ($n=0; $n < count($contractInfo[0]); $n++) {
		   		$contractInfoTwoDate[] = $contractInfo[0][$n];
		   	}
		   	//累积服务时长
		   	$servicedDay = $contractInfo[1];
	   	}else{
	   		$contractInfoTwoDate = array();
	   		$servicedDay = 0;
	   	}
	   	if($custoInfo){
			$res['rcode'] = 1;
			$res['custoInfo'] = $custoInfo;
			$res['combinFromInfo'] = $combinFromInfos;
			$res['onServicecCntract'] = $onServicecCntract;
			$res['onServiceCount'] = count($onServicecCntract);
			$res['payInfoTwoDate'] = $payInfoTwoDate;
			$res['payCount'] = count($payInfo);
			$res['contractInfoTwoDate'] = $contractInfoTwoDate;
			$res['contractCount'] = count($contractInfo[0]);
			$res['servicedDay'] = $servicedDay;
		}else{
			$res['msg'] = '客户信息未获取！';
		}
		e_json($res);
	}

	/*获取客户基本信息tab*/
	public function customerInfo(){
		$res = array('rcode'=>0,'msg'=>'OK');
		$customer_id = I('get.id',false,'intval');
		$wheres1 = array();
		$wheres1['a.customer_id'] = array('eq',$customer_id);
		$wheres1['_string'] = '1=1';
		//客户基本信息
		$infos = M()->field("a.customer_id,a.nickname as customername,a.phone,a.location,a.mount,a.customer_code,
						a.qq,a.weixin_id,a.career,a.education,a.birthday,a.address,a.remark,a.source_id,a.sales_person,
						b.name as source_name,d.nickname as sales_name,e.sector_name as sub_company_name,f.customer_id as combineto_id,f.nickname as combineto_name,a.sex,
						CASE  WHEN a.sex = 0  THEN '未知'
						WHEN a.sex = 1  THEN '男'
						WHEN a.sex = 2  THEN '女'
						ELSE '未知' END AS sexname")
					->table('customer as a')
					->join('LEFT JOIN source as b on b.source_id = a.source_id','LEFT')
					->join('LEFT JOIN user as d on d.user_id = a.sales_person','LEFT')
					->join('LEFT JOIN sector as e on e.sector_id = a.sales_sub_company','LEFT')
					->join('LEFT JOIN customer as f on f.customer_id = a.combine_to','LEFT')
					->where($wheres1)
					->find();

		if($infos['birthday'])		$infos['birthday'] = substr($infos['birthday'],0,10);
		//电话号码保存历史
		$maps = array();
		$maps['customer_id'] = array('eq', $customer_id);
		$phoneInfo = M('customer_phonebook')->field('*')->where($maps)->order("book_id desc")->select();
		for ($i=0; $i < count($phoneInfo); $i++) {
			$phoneInfo[$i]['i'] = $i+1;
		}
		if($infos){
			$res['rcode'] = 1;
			$res['infos'] = $infos;
			$res['phoneInfo'] = $phoneInfo;
		}else{
			$res['msg'] = '客户信息未获取！';
		}
		e_json($res);
	}

	/*修改客户基本信息*/
	public function editCust(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$editCustIn = D('Customer')->updateCustomer($_POST);
		if( $editCustIn['r'] ){
			$res['rcode'] = 1;
			$res['msg'] = 'OK';
			$res['data'] = $editCustIn['d'];
		}else{
			$res['msg'] = $editCustIn['i'];
		}
		e_json( $res );
	}

	/**添加沟通信息*/
	public function addContact(){
		$res = array('rcode'=>0,'data'=>NULL,'msg'=>'OK');
		$customer_id = I('post.customer_id','','intval');
		$content = I('post.content','','trim');
		$dlist = D('Customer')->add_contact($customer_id,$content);
		if($dlist){
			$res['rcode'] = 1;
			$res['data'] = $dlist['d'];
		}else{
			$res['msg'] =$dlist['m'];
		}
		e_json($res);
	}

	/*付款信息*/
	public function getFukuan(){
		$psize = I('get.psize', $this->m_psize,'intval');
		$customer_id = I('get.customer_id');
		$res = array('rcode'=>0, 'data'=>NULL, 'msg'=> 'OK');
		if( !$psize )
			$psize = $this->m_psize;
		else if( $psize > 20 )
			$psize = $this->m_psize;
		if (!$customer_id) {
			$res['msg']=L("客户信息未获取");
			e_json($res);
		}
		//获取付款信息
	   	$payInfoDate = D('Customer')->getPayInfos($customer_id);
	   	//分页计算 
      	$count = count($payInfoDate);
      	$Page  = new \Common\ORG\Page( $count, $psize );
      	$p = $Page->nowPage;
      	for($i =($p-1)*$psize;$i<$p*$psize;$i++){
          	$calculate[$i] = $payInfoDate[$i];
       	}
       	foreach ($calculate as $v) {
          	if(!empty($v)){
              	$resultDate[] = $v;
          	}
       	}
	   	if($payInfoDate){
	   		$res['rcode'] = 1;
			$res['data'] = $resultDate;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
			$res['cnt'] = $count;
	   	}else{
	   		$res['rcode'] = 2;
	   		$res['msg'] = '暂无数据！';
	   	}
		e_json($res);
	}

	/*合同信息*/
	public function getContractList(){
		$psize = I('get.psize', $this->m_psize,'intval');
		$customer_id = I('get.customer_id');
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		if( !$psize )
			$psize = $this->m_psize;
		else if( $psize > 20 )
			$psize = $this->m_psize;
		if (!$customer_id) {
			$res['msg']=L("客户信息未获取");
			e_json($res);
		}
		//获取当前客户及其归并客户id
	   	$contractInfo = D('Customer')->getContractInfos($customer_id);
	   	$contractInfoDate = $contractInfo[0];
	   	//分页计算 
      	$count = count($contractInfoDate);
      	$Page  = new \Common\ORG\Page( $count, $psize );
      	$p = $Page->nowPage;
      	for($i =($p-1)*$psize;$i<$p*$psize;$i++){
          	$calculate[$i] = $contractInfoDate[$i];
       	}
       	foreach ($calculate as $v) {
          	if(!empty($v)){
              	$resultDate[] = $v;
          	}
       	}
	   	if($contractInfoDate){
	   		$res['rcode'] = 1;
			$res['data'] = $resultDate;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
			$res['count'] = $count;
	   	}else{
	   		$res['rcode'] = 2;
	   		$res['msg'] = '暂无数据！';
	   	}
		e_json($res);
	}

	/**/
	public function getRight(){
		$ServiceUser = D("Admin/User","Service","Admin");
		$arr['fengkong'] = $ServiceUser->authWithKey($this->m_user['user_id'],'VIEW360_FENGKONG');//风控权限
		$arr['baseEdit'] = $ServiceUser->authWithKey($this->m_user['user_id'],'VIEW360_BASEEDIT');//编辑基本信息权限
		$arr['sensiEdit'] = $ServiceUser->authWithKey($this->m_user['user_id'],'VIEW360_SENSIEDIT');//编辑敏感信息权限
		$arr['saleperEdit'] = $ServiceUser->authWithKey($this->m_user['user_id'],'VIEW360_SALESPERSONEDIT');//编辑服务人员权限
		if($this->m_user['user_id'] == 1){
			$arr['rcode'] = 1;
			$arr['fengkong']['rcode'] = 1;
			$arr['baseEdit']['rcode'] = 1;
			$arr['sensiEdit']['rcode'] = 1;
			$arr['saleperEdit']['rcode'] = 1;
		}
		e_json($arr);
	}

	/*获取400记录*/
	public function getComplainInfo(){
		$res = array('rcode'=>0, 'data'=>NULL, 'msg'=> 'OK', 'cnt'=>0);
		$psize = I('get.psize', 3,'intval');
		$customer_id = I('get.customer_id');
		if( !$psize )
			$psize = $this->m_psize;
		else if( $psize > 3 )
			$psize = $this->m_psize;
		if (!$customer_id) {
			$res['msg'] = "客户信息未获取";
		}
		$wheres = array();
		$wheres['customer_id'] = array('eq',$customer_id);
		$wheres['is_delete'] = array('neq',1);

		$cnt = M('customer_complain')->where( $wheres )->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );

		$infos = M('customer_complain')
				->field('incoming_type,up_time,
					CASE  WHEN incoming_type = 11  THEN "日常事务"
					WHEN incoming_type = 21  THEN "广告"
					WHEN incoming_type = 31  THEN "投诉"
					ELSE "其它" END AS incoming_name,content,create_time,status,
					CASE  WHEN status = 11  THEN "处理中"
					WHEN status = 21  THEN "已解决"
					WHEN status = 31  THEN "已挂起"
					ELSE "未处理" END AS status_name')
				->where($wheres)
				->order('create_time desc')
				->limit($Page->firstRow.','.$Page->listRows )
				->select();
		for ($i=0; $i < count($infos); $i++) {
			if($infos[$i]['create_time'])
				$infos[$i]['create_time'] = date('Y-m-d',strtotime($infos[$i]['create_time']));
			if($infos[$i]['up_time'])
				$infos[$i]['up_time'] = date('Y-m-d',strtotime($infos[$i]['up_time']));
		}
		if($infos){
			$res['data'] = $infos;
			$res['cnt'] = $cnt;
			$res['rcode'] = 1;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}else{
			$res['msg'] = '获取400记录失败！';
			$res['cnt'] = 0;
		}
		e_json($res);
	}

	/*获取追踪记录信息风控+400*/
	public function getRiskInfo(){
		$res = array('rcode'=>0, 'data'=>NULL, 'msg'=> 'OK', 'cnt'=>0);
		$psize = I('get.psize', 3,'intval');
		$customer_id = I('get.customer_id');

		if( !$psize )
			$psize = $this->m_psize;
		else if( $psize > 3 )
			$psize = $this->m_psize;
		if (!$customer_id) {
			$res['msg'] = "客户信息未获取";
		}
		$wheres1 = array();
		$wheres1['risk.customer_id'] = array('eq',$customer_id);
		$wheres1['risk.is_delete'] = array('neq',1);
		$wheres1['risk_track.is_delete'] = array('neq',1);

		$riskTrackInfos = M('risk_track')
					->field('risk_track.confirm_time,risk.description,risk_track.reply,risk_track.reply_confirm,
						risk.create_time,risk_track.last_status,risk_track.risk_track_id,	
						CASE  WHEN risk_track.last_status = 1  THEN "处理中"
						WHEN risk_track.last_status = 2  THEN "已解决"
						WHEN risk_track.last_status = 3  THEN "已挂起"
						ELSE "未处理" END AS status_name')
					->join("LEFT JOIN risk on risk.risk_id=risk_track.risk_id")
					->where($wheres1)
					->order('risk.create_time desc')
					->select();
				
		$wheres2 = array();
		$wheres2['complain.customer_id'] = array('eq',$customer_id);
		$wheres2['complain.is_delete'] = array('neq',1);
		$wheres2['reply.is_delete'] = array('neq',1);

		$complainReplyInfo = M('customer_complain_reply')->alias('reply')
					->field('reply.create_time as confirm_time,complain.content as description,reply.content as reply_confirm,
					complain.create_time,reply.last_status,reply.complain_reply_id,
					CASE  WHEN reply.last_status = 11  THEN "处理中"
					WHEN reply.last_status = 21  THEN "已解决"
					WHEN reply.last_status = 30  THEN "已挂起"
					ELSE "未处理" END AS status_name')
				->join("LEFT JOIN customer_complain complain on complain.complain_id=reply.complain_id")
				->where($wheres2)
				->order('reply.create_time desc')
				->select();
		if($riskTrackInfos){
			$i = count($riskTrackInfos);
			if($complainReplyInfo){
				for ($j=0; $j < count($complainReplyInfo); $j++) {
					$riskTrackInfos[$i]['confirm_time'] = $complainReplyInfo[$j]['confirm_time'];
					$riskTrackInfos[$i]['description'] = $complainReplyInfo[$j]['description'];
					$riskTrackInfos[$i]['reply'] = '无';
					$riskTrackInfos[$i]['reply_confirm'] = $complainReplyInfo[$j]['reply_confirm'];
					$riskTrackInfos[$i]['create_time'] = $complainReplyInfo[$j]['create_time'];
					$riskTrackInfos[$i]['status_name'] = $complainReplyInfo[$j]['status_name'];
					$i++;
				}
				
			}
		}
		foreach ($riskTrackInfos as $key => $row) {
		    $confirm_time[$key]  = $row['confirm_time'];
		}
		array_multisort($confirm_time, SORT_DESC, $riskTrackInfos);	//排序
		for($i=0; $i < count($riskTrackInfos); $i++) {
			if($riskTrackInfos[$i]['confirm_time'])
				$riskTrackInfos[$i]['confirm_time'] = date('Y-m-d',strtotime($riskTrackInfos[$i]['confirm_time']));
			if($riskTrackInfos[$i]['create_time'])
				$riskTrackInfos[$i]['create_time'] = date('Y-m-d',strtotime($riskTrackInfos[$i]['create_time']));
		}
		//分页计算 
      	$count = count($riskTrackInfos);
      	$Page  = new \Common\ORG\Page( $count, $psize );
      	$p = $Page->nowPage;
      	for($i =($p-1)*$psize;$i<$p*$psize;$i++){
      		if($riskTrackInfos[$i]['create_time'])
          		$calculate[$i] = $riskTrackInfos[$i];
       	}
		if($calculate){
			$res['data'] = $calculate;
			$res['cnt'] = $count;
			$res['rcode'] = 1;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}else{
			$res['msg'] = '获取风险追踪记录失败！';
			$res['cnt'] = 0;
		}
		e_json($res);
	}

	/*获取日常通话记录信息*/
	public function getPhoneInfo(){
		$res = array('rcode'=>0, 'data'=>NULL, 'msg'=> 'OK', 'cnt'=>0);
		$psize = I('get.psize', 3,'intval');
		$customer_id = I('get.customer_id');
		if( !$psize )
			$psize = $this->m_psize;
		else if( $psize > 3 )
			$psize = $this->m_psize;
		if (!$customer_id) {
			$res['msg'] = "客户信息未获取";
		}

		$wheres = array();
		$wheres['cu.customer_id'] = array('eq',$customer_id);

		$cnt = M('customer_phonebook')->alias("book")
				->join('calllog ca on ca.telephone=book.book_phone')
				->join('customer cu on book.customer_id=cu.customer_id')
				->where( $wheres )->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );
		$callInfo = M('customer_phonebook')->alias("book")
					->field('ca.telephone,ca.begintime,ca.endtime,ca.calltime,ca.admin_no')
					->join('calllog ca on ca.telephone=book.book_phone')
					->join('customer cu on book.customer_id=cu.customer_id')
					->where($wheres)
					->order('ca.begintime desc')
					->limit($Page->firstRow.','.$Page->listRows )
					->select();
		for ($i=0; $i < count($callInfo); $i++) {
			$map[$i] = array();
			$map[$i]['user_id'] = array('eq',$callInfo[$i]['admin_no']);
			$nickname[$i] = M("User")->where($map[$i])->getField('nickname');
			$callInfo[$i]['nickname'] = $nickname[$i];
			if($callInfo[$i]['calltime'] && $callInfo[$i]['calltime']>60)
				$callInfo[$i]['calltime'] = floor($callInfo[$i]['calltime']/60)."''".($callInfo[$i]['calltime']%60)."'";
			else if($callInfo[$i]['calltime'] && $callInfo[$i]['calltime']<=60)
				$callInfo[$i]['calltime'] = $callInfo[$i]['calltime']."'";
		}
		if($callInfo){
			$res['data'] = $callInfo;
			$res['cnt'] = $cnt;
			$res['rcode'] = 1;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}else{
			$res['msg'] = '获取日常通话记录失败！';
			$res['cnt'] = 0;
		}
		e_json($res);
	}

	/*获取日常沟通记录信息*/
	public function getContactInfo(){
		$res = array('rcode'=>0, 'data'=>NULL, 'msg'=> 'OK', 'cnt'=>0);
		$psize = 3;
		$customer_id = I('get.customer_id');
		if (!$customer_id) {
			$res['msg'] = "客户信息未获取";
		}

		$wheres = array();
		$wheres['c.customer_id'] = array('eq', $customer_id);

		$cnt = M('customer_contact_log')->alias("c")
				->join("user u on u.user_id=c.sales_id")
				->where( $wheres )
				->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );

		$contactInfo = M('customer_contact_log')->alias("c")
						->field('c.create_time,u.nickname as contact_name,c.content,c.sales_id,c.contact_id')
						->join("user u on u.user_id=c.sales_id")
						->where($wheres)
						->order("c.create_time desc")
				  		->limit($Page->firstRow.','.$Page->listRows )
						->select();
		for ($i=0; $i < count($contactInfo); $i++) { 
			if($contactInfo[$i]['create_time'])
				$contactInfo[$i]['create_time'] = date('Y-m-d',strtotime($contactInfo[$i]['create_time']));
		}

		if($contactInfo){
			$res['data'] = $contactInfo;
			$res['cnt'] = $cnt;
			$res['rcode'] = 1;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}else{
			$res['msg'] = '沟通记录失败！';
		}
		e_json($res);
	}

	/*判断电话号码是否已经存在*/
	public function checkPhone(){
		$res = array('rcode'=>0, 'data'=>NULL, 'msg'=> 'OK');
		$customer_id = I('post.customer_id');
		$phone = I('post.phone','','trim');
		$wheres = array();
		$wheres['book_phone'] = array('eq',$phone);
		$wheres['customer_id'] = array('neq',$customer_id);
		$phoneExist = M('customer_phonebook')->where($wheres)->find();
		if($phoneExist){
			$res['rcode'] = 1;
			$res['msg'] = '已经存在';
		}
		e_json($res);
	}

	/*修改客户服务人员*/
	public function editSalesPerson(){
		$res = array('rcode'=>0, 'data'=>NULL, 'msg'=> 'OK');
		$customer_id = I('get.customer_id',false,'intval');
		$sales_person = I('get.sales_person',false,'intval');
		$sales_sub_company = I('get.sales_sub_company',false,'intval');
		$wheres1 = array();
        $wheres1['customer_id'] = array('eq', $customer_id );
        $wheres1['is_delete'] = array('neq', 1);
        $editCustomer = M('Customer')->where( $wheres1 )->find();
        if($customer_id){
			if( $editCustomer ){
	        	$d = array();
	            $d['update_time'] = date('Y-m-d H:i:s');
	            $d['sales_person'] = $sales_person;
	            $d['sales_sub_company'] = $sales_sub_company;
	            $editID = M("customer")->where( $wheres1 )->save( $d );
	            if($editID || $editID==0){
					$res['data'] = $editID;
					$res['rcode'] = 1;
				}else
					$res['msg'] = '编辑失败';
			}else
				$res['msg'] = '抱歉，数据不存在！';
        }else
        	$res['msg'] = '参数错误！';
        
		e_json($res);
	}

	/*编辑服务人员-获取服务人员列表*/
	public function getSalePersonInfo(){
		$res = array('rcode'=>0, 'data'=>NULL, 'msg'=> 'OK');
		$nickname = I('get.nickname','','trim');
		$eq = I('get.eq');
		$wheres = array();
		$wheres['user.is_deleted'] = array('neq',1);
		if(!$eq && $nickname)
			$wheres['user.nickname'] = array('like','%'.$nickname.'%');
		else if($eq)
			$wheres['user.nickname'] = array('eq',$nickname);
		$salesPersonList = M("Sector")->field("user.user_id,user.nickname,user.sector_id,sector.is_subcompany,
						sector.sector_name,sector.parent_sector,sector.parent_company")
						->join("user on user.sector_id=sector.sector_id")
						->where($wheres)
						->select();
		if($salesPersonList){
			for ($i=0; $i < count($salesPersonList); $i++) { 
				if($salesPersonList[$i]['is_subcompany'] == 1 || $salesPersonList[$i]['parent_sector'] == 0){	//是分公司或总公司
					$salesPersonList[$i]['sub_companyname'] = $salesPersonList[$i]['sector_name'];
					$salesPersonList[$i]['parent_company'] = $salesPersonList[$i]['sector_id'];
				}else{		//分公司下属部门，查所属分公司
					$maps[$i] = array();
					$maps[$i]['sector.sector_id'] = $salesPersonList[$i]['sector_id'];
					$parent_companyname[$i] = M("Sector")->field("s.sector_name as sub_companyname")
									->join("sector s on sector.parent_company=s.sector_id")
									->where($maps[$i])
									->find();
					if($parent_companyname[$i]){
						$salesPersonList[$i]['sub_companyname'] = $parent_companyname[$i]['sub_companyname'];
					}
				}
			}
		}
		if($salesPersonList){
			$res['data'] = $salesPersonList;
			$res['rcode'] = 1;
		}else{
			$res['msg'] = 'no data';
		}
		e_json($res);
	}

	public function getWenjuanInfo(){
		$DSurvey = M("customer_survey");
		$res = array('rcode'=>0, 'data'=>NULL, 'msg'=> 'OK','cnt'=>0);
		$psize = 3;
		$customer_id = I('get.customer_id');
		if (!$customer_id) {
			$res['msg'] = "客户信息未获取";
			return $res;
		}
		$wheres = array();
		$wheres['customer_id'] = array('eq', $customer_id);
		$cnt = $DSurvey->where($wheres)->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );
		$surveyList = $DSurvey
					->where($wheres)
					->order("create_time desc")
			  		->limit($Page->firstRow.','.$Page->listRows )
					->select();
		for ($j=0; $j < count($surveyList); $j++) { 
			if($surveyList[$j]['rank'])
                $surveyList[$j]['rank'] = floor($surveyList[$j]['rank']);
		}
		if($surveyList){
			$res['data'] = $surveyList;
			$res['cnt'] = $cnt;
			$res['rcode'] = 1;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}else{
			$res['msg'] = '获取问卷记录失败！';
		}
		e_json($res);
	}


}
?>
